package com.boot.mall.product.service.impl;

import com.boot.mall.product.vo.Images;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.boot.common.utils.PageUtils;
import com.boot.common.utils.Query;

import com.boot.mall.product.dao.SpuImagesDao;
import com.boot.mall.product.entity.SpuImagesEntity;
import com.boot.mall.product.service.SpuImagesService;


@Service("spuImagesService")
public class SpuImagesServiceImpl extends ServiceImpl<SpuImagesDao, SpuImagesEntity> implements SpuImagesService {
    // 批量保存spu的图片信息
    @Override
    public void saveImages(Long id, List<String> images) {

        List<SpuImagesEntity> list = new ArrayList<>();

        for (String image : images) {
            // 剔除商品路径为空的图片信息
            if (!StringUtils.isEmpty(image)) {
                SpuImagesEntity spuImagesEntity = new SpuImagesEntity();
                // 设置默认图片
//            spuImagesEntity.setDefaultImg(image.getDefaultImg());
                // 设置图片地址
                spuImagesEntity.setImgUrl(image);
                // 拼接图片名字   2c4e255f4af44341b294aaf2d482b083.jpg
                // https://huihui-avatar.oss-cn-hangzhou.aliyuncs.com/guli-shop/2c4e255f4af44341b294aaf2d482b083.jpg
                spuImagesEntity.setImgName(image.substring(image.lastIndexOf("/") + 1, image.length()));
                // 设置spu的id
                spuImagesEntity.setSpuId(id);
                list.add(spuImagesEntity);
            }
        }
        // 批量保存
        this.saveBatch(list);
    }
}